All files / web/src/app/api/abacus/print/connections/[id] route.ts

0% Statements 0/48
0% Branches 0/1
0% Functions 0/1
0% Lines 0/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                                                                 
/**
 * A single print-service connection (Abacus Studio Phase 2a, #8.2)
 *
 * PATCH  /api/abacus/print/connections/[id] - rename
 * DELETE /api/abacus/print/connections/[id] - disconnect (row only; the
 *   service-side token stays valid until revoked in the service's admin)
 */
import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserId } from '@/lib/viewer'
import { deleteConnection, renameConnection } from '@/lib/abacus/print/connections'

export const PATCH = withAuth(async (request, { params }) => {
  try {
    const { id } = (await params) as { id: string }
    const userId = await getUserId()
    const body = await request.json().catch(() => ({}))
    const name = typeof body.name === 'string' ? body.name.trim() : ''
    if (!name) {
      return NextResponse.json({ error: 'Name is required' }, { status: 400 })
    }

    const updated = await renameConnection(userId, id, name)
    if (!updated) {
      return NextResponse.json({ error: 'Connection not found' }, { status: 404 })
    }
    return NextResponse.json({ connection: updated })
  } catch (error) {
    console.error('[print-connections] rename failed:', error)
    return NextResponse.json({ error: 'Failed to rename connection' }, { status: 500 })
  }
})

export const DELETE = withAuth(async (_request, { params }) => {
  try {
    const { id } = (await params) as { id: string }
    const userId = await getUserId()

    const deleted = await deleteConnection(userId, id)
    if (!deleted) {
      return NextResponse.json({ error: 'Connection not found' }, { status: 404 })
    }
    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('[print-connections] delete failed:', error)
    return NextResponse.json({ error: 'Failed to delete connection' }, { status: 500 })
  }
})